package com.huifaapp.mall.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.OrderdetailsAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.OrderdetailsBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class OrderdetailsActivity extends AppCompatActivity {

    private RecyclerView Reorder;
    //   private List<PendingCommBean.DataBean> goods;
    private TextView recename,recephone,receaddress,runprice,redbag,sjprice;
    private TextView creatpay,ddnum,zfjynum;
    private OrderdetailsBean.DataBean goods;
    private Button bt_delete;
    private List<OrderdetailsBean.DataBean.OrderItemBean> orderdata;
    private LinearLayout linearLayout;
    private Button bt_kefu;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderdetails);
        initView();
        // initData();

    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = getIntent();
        String orderNo = intent.getStringExtra("orderNo");
        Log.e("待评价详情url---------",ApiManager.BASE_NIUPAI_URL+"orders/notjudgeddetail?"+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(OrderdetailsActivity.this)+"&orderNo="+orderNo);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notjudgeddetail")
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(OrderdetailsActivity.this))
                .addParams("orderNo",orderNo)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {

                try {

                    Gson gson = new Gson();
                    OrderdetailsBean orderdetailsBean = gson.fromJson(response,OrderdetailsBean.class);
                    goods = orderdetailsBean.getData();
                    orderdata = orderdetailsBean.getData().getOrderItem();
                    //               OrderdetailsAdapter orderdetailsAdapter = new OrderdetailsAdapter(OrderdetailsActivity.this,goods,orderdata);
//                    Reorder.setLayoutManager(new LinearLayoutManager(OrderdetailsActivity.this));
//                    Reorder.setAdapter(orderdetailsAdapter);
                    createShopItem(linearLayout,orderdata);
                    recename.setText(orderdetailsBean.getData().getReceiverName());
                    recephone.setText(orderdetailsBean.getData().getReceiverPhone()+"");
                    receaddress.setText(orderdetailsBean.getData().getReceiverAddress());
                    runprice.setText(orderdetailsBean.getData().getExpressfee()+"");
                    redbag.setText(orderdetailsBean.getData().getCouponAmount()+"");
                    sjprice.setText(orderdetailsBean.getData().getActualpay()+"");
                    zfjynum.setText(orderdetailsBean.getData().getPayno()+"");
                    ddnum.setText(orderdetailsBean.getData().getOrderNo()+"");
                    creatpay.setText(orderdetailsBean.getData().getCreatetime()+"");
                }catch (Exception e){
                    ToastUtils.showShort("huoqushibai");
                }

            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Intent intent = getIntent();
        String orderNo = intent.getStringExtra("orderNo");
        Log.e("待评价详情url---------",ApiManager.BASE_NIUPAI_URL+"orders/notjudgeddetail?"+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(OrderdetailsActivity.this)+"&orderNo="+orderNo);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notjudgeddetail")
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(OrderdetailsActivity.this))
                .addParams("orderNo",orderNo)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {

                try {

                    Gson gson = new Gson();
                    OrderdetailsBean orderdetailsBean = gson.fromJson(response,OrderdetailsBean.class);
                    goods = orderdetailsBean.getData();
                    orderdata = orderdetailsBean.getData().getOrderItem();
                    //               OrderdetailsAdapter orderdetailsAdapter = new OrderdetailsAdapter(OrderdetailsActivity.this,goods,orderdata);
//                    Reorder.setLayoutManager(new LinearLayoutManager(OrderdetailsActivity.this));
//                    Reorder.setAdapter(orderdetailsAdapter);
                    createShopItem(linearLayout,orderdata);
                    recename.setText(orderdetailsBean.getData().getReceiverName());
                    recephone.setText(orderdetailsBean.getData().getReceiverPhone()+"");
                    receaddress.setText(orderdetailsBean.getData().getReceiverAddress());
                    runprice.setText(orderdetailsBean.getData().getExpressfee()+"");
                    redbag.setText(orderdetailsBean.getData().getCouponAmount()+"");
                    sjprice.setText(orderdetailsBean.getData().getActualpay()+"");
                    zfjynum.setText(orderdetailsBean.getData().getPayno()+"");
                    ddnum.setText(orderdetailsBean.getData().getOrderNo()+"");
                    creatpay.setText(orderdetailsBean.getData().getCreatetime()+"");
                }catch (Exception e){
                    ToastUtils.showShort("huoqushibai");
                }

            }
        });
    }

    private void initView() {
        //  Reorder = (RecyclerView) findViewById(R.id.orderdetails_rv);
        recename = (TextView) findViewById(R.id.orderdetails_recename);
        recephone = (TextView) findViewById(R.id.orderdetails_recephone);
        receaddress = (TextView) findViewById(R.id.orderdetails_address);
        runprice = (TextView) findViewById(R.id.orderdetails_runprice);
        redbag = (TextView) findViewById(R.id.orderdetails_redbag);
        sjprice = (TextView) findViewById(R.id.orderdetails_sjprice);
        ddnum = (TextView) findViewById(R.id.orderdetails_ddnum);
        zfjynum = (TextView) findViewById(R.id.orderdetails_zfjynum);
        creatpay = (TextView) findViewById(R.id.orderdetails_createtime);
        bt_delete = (Button) findViewById(R.id.orderdetails_delete);
        bt_kefu = (Button) findViewById(R.id.orderdetails_kefu);
        //bt_tk = (Button) findViewById(R.id.orderdetails_bttk);
        linearLayout = (LinearLayout) findViewById(R.id.orderdetails_ll);
        Intent intent = getIntent();
        String orderNo = intent.getStringExtra("orderNo");
        Log.e("待评价详情url---------",ApiManager.BASE_NIUPAI_URL+"orders/notjudgeddetail?"+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(OrderdetailsActivity.this)+"&orderNo="+orderNo);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notjudgeddetail")
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(OrderdetailsActivity.this))
                .addParams("orderNo",orderNo)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {

                try {

                    Gson gson = new Gson();
                    OrderdetailsBean orderdetailsBean = gson.fromJson(response,OrderdetailsBean.class);
                    goods = orderdetailsBean.getData();
                    orderdata = orderdetailsBean.getData().getOrderItem();
                    //               OrderdetailsAdapter orderdetailsAdapter = new OrderdetailsAdapter(OrderdetailsActivity.this,goods,orderdata);
//                    Reorder.setLayoutManager(new LinearLayoutManager(OrderdetailsActivity.this));
//                    Reorder.setAdapter(orderdetailsAdapter);
                    createShopItem(linearLayout,orderdata);
                    recename.setText(orderdetailsBean.getData().getReceiverName());
                    recephone.setText(orderdetailsBean.getData().getReceiverPhone()+"");
                    receaddress.setText(orderdetailsBean.getData().getReceiverAddress());
                    runprice.setText(orderdetailsBean.getData().getExpressfee()+"");
                    redbag.setText(orderdetailsBean.getData().getCouponAmount()+"");
                    sjprice.setText(orderdetailsBean.getData().getActualpay()+"");
                    zfjynum.setText(orderdetailsBean.getData().getPayno()+"");
                    ddnum.setText(orderdetailsBean.getData().getOrderNo()+"");
                    creatpay.setText(orderdetailsBean.getData().getCreatetime()+"");
                }catch (Exception e){
                    ToastUtils.showShort("huoqushibai");
                }

            }
        });

        bt_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AlertDialog alertDialog = new AlertDialog.Builder(OrderdetailsActivity.this).setTitle("提示").setMessage("是否需要删除？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                final String id = goods.getOrderNo();
                                OkHttpUtils.post().url(ApiManager.BASE_NIUPAI_URL+"orders/delorders")
                                        .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                                        .addParams("appId", AUniqueIdentifier.getDeviceId(OrderdetailsActivity.this))
                                        .addParams("orderNo",id)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {
                                                ToastUtils.showShort("删除失败");
                                            }

                                            @Override
                                            public void onResponse(String response) {
                                                Log.e("HB",response);
                                                try {
                                                    JSONObject jsonObject = new JSONObject(response);
                                                    String code =  jsonObject.getString("code");
                                                    if (code.equals("10000"))
                                                    {
                                                        ToastUtils.showShort("删除成功");
                                                        orderdata.remove(goods.getOrderNo());
                                                    }else {
                                                        ToastUtils.showShort("删除失败");
                                                    }

                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }


                                            }
                                        });

                            }
                        }).create();
                alertDialog.show();

            }

        });
    }


    private void createShopItem(ViewGroup viewGroup, List<OrderdetailsBean.DataBean.OrderItemBean> lists){
        if (lists == null) {
            return;
        }
        viewGroup.removeAllViews();
        for (int i = 0; i < lists.size(); i++) {

            final OrderdetailsBean.DataBean .OrderItemBean orderItemBean = orderdata.get(i);
            Log.e("售后orderno",goods.getOrderNo());
            Log.e("售后sku",orderItemBean.getSkuId());
            View view= LayoutInflater.from(this).inflate(R.layout.orderdetails_item,null);
            final OrderdetailsAdapter.MyViewHolder viewHolder = new OrderdetailsAdapter.MyViewHolder(view);
            final String dealstate = orderItemBean.getDealState();

            Log.e("shouhuoxiangqing",dealstate);

            if (dealstate.equals("001")){
                viewHolder.bt_money.setText("退款中");
            }else if (dealstate.equals("002")){
                viewHolder.bt_money.setText("换货关闭/退货关闭");
            }else if (dealstate.equals("003")){
                viewHolder.bt_money.setText("待处理");
            }else if (dealstate.equals("004")){
                viewHolder.bt_money.setText("售后拒绝");
            }else if (dealstate.equals("005")){
                viewHolder.bt_money.setText("同意换货填写物流");
            }else if (dealstate.equals("006")){
                viewHolder.bt_money.setText("退款成功");
            }


            //退款按钮
            viewHolder.bt_money.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.e("dealstate",dealstate );
                    if (dealstate.equals("0")){
                        Intent intent = new Intent(OrderdetailsActivity.this,AfterSaleStateActivity.class);
                        intent.putExtra("orderNo",goods.getOrderNo());
                        intent.putExtra("skuId",orderItemBean.getSkuId()+"");
                        intent.putExtra("goodsid",orderItemBean.getGoodsId()+"");
                        intent.putExtra("state",orderItemBean.getState()+"");
                        startActivity(intent);
                    }else {
//                        Intent intent = new Intent(OrderdetailsActivity.this,AfterStatusActivity.class);
//                        intent.putExtra("aftersaleId",orderItemBean.getAftersaleId()+"");
//                        Log.e("daishouhuoxq",orderItemBean.getAftersaleId()+"");
//                        startActivity(intent);
                    }
                }
            });

            bt_kefu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(OrderdetailsActivity.this, com.huifaapp.mall.activity.Homefragment.WebActivity.class);
                    intent.putExtra("url",ApiManager.KEFU);
                    startActivity(intent);
                }
            });

            //评价按钮
            viewHolder.bt_comm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.e("评论订单号",orderItemBean.getOrderNo());
                    Intent intent = new Intent(OrderdetailsActivity.this,AssessActivity.class);
                    intent.putExtra("skuid",orderItemBean.getSkuId()+"");
                    intent.putExtra("orderId",orderItemBean.getOrderNo());
                    startActivity(intent);
                }
            });

            viewHolder.setData(this, lists.get(i));
            viewGroup.addView(view);
        }
    }
}
